package com.java.business.system.permis.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.system.permis.bean.Menu;
import com.java.business.system.permis.service.MenuService;
import com.java.core.bean.LowerCaseKeyMap;
import com.java.core.bean.ResultBean;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/system/permis/menu")
@Controller
public class MenuController {
	@Autowired
	private MenuService service;

	@RequestMapping("/add")
	public void add(HttpServletResponse response, Menu menu) {
		ResultBean rb = new ResultBean();
		try {
			service.add(menu);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/update")
	public void update(HttpServletResponse response, Menu menu) {
		ResultBean rb = new ResultBean();
		try {
			service.update(menu);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, Menu menu) {
		ResultBean rb = new ResultBean();
		try {
			service.delete(menu);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setDisabled")
	public void setDisabled(HttpServletResponse response, Menu menu) {
		ResultBean rb = new ResultBean();
		try {
			service.setDisabled(menu);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setReplace")
	public void setReplace(HttpServletResponse response, String[] menuids,
			String parentid) {
		ResultBean rb = new ResultBean();
		try {
			service.setReplace(menuids, parentid);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getMenuList")
	public void getMenuList(HttpServletResponse response, Menu menu)
			throws Exception {
		List<LowerCaseKeyMap> list = service.getMenuList(menu);
		ResponseUtils.renderJson(response, JSON.toJSONString(list));
	}

	@RequestMapping("/getMenuListByRole")
	public void getMenuListByRole(HttpServletResponse response, String roleid,
			String parentid) {
		List<LowerCaseKeyMap> list = service
				.getMenuListByRole(roleid, parentid);
		ResponseUtils.renderJson(response, JSON.toJSONString(list));
	}

	@RequestMapping("/getMenuListByUser")
	public void getMenuListByUser(HttpServletResponse response, String userid,
			String parentid) {
		List<LowerCaseKeyMap> list = service
				.getMenuListByUser(userid, parentid);
		ResponseUtils.renderJson(response, JSON.toJSONString(list));
	}
}
